home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Sample Code / Graphics Samples / ShapePart Browser ƒ / Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-15  |  8.1 KB  |  483 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    Main.c 
  3.  *
  4.  *    Robert Dierkes,  April 26, 1993
  5.  */
  6.  
  7. /*------------------*/
  8. /*    Include Files    */
  9. /*------------------*/
  10. #include "graphics toolbox.h"
  11. #include "graphics libraries.h"
  12. #include "graphics debugging.h"
  13. #include "graphics macintosh.h"
  14.  
  15. #include "ResourceIDs.h"
  16. #include "GestaltEqu.h"
  17. #include "Window.h"
  18. #include "ShapeAction.h"
  19. #include "AutoMouse.h"
  20.  
  21. #include "Main.h"
  22.  
  23.  
  24. /*----------------------*/
  25. /*    Global Declarations    */
  26. /*----------------------*/
  27. gxGraphicsClient    gClient;
  28. Boolean            gQuitApp;
  29. Handle            ghMainMenu;
  30. Rect            gDragRect;
  31. WindowPtr        gWindow;
  32.  
  33.  
  34. /*------------------------------*/
  35. /*    External Declarations     */
  36. /*------------------------------*/
  37. extern    GlobalStructure    globals;
  38.  
  39.  
  40.     Boolean
  41. InitApp (void)
  42. {
  43.     long            response;
  44.     gxGraphicsError    grfxError;
  45.  
  46.     if (Gestalt (gestaltGraphicsVersion, &response)) {
  47.         DebugStr ("\pQuickDraw GX is not installed");
  48.         return false;
  49.     }
  50.  
  51.     if ((gClient = GXNewGraphicsClient (nil, kGraphicsHeapSize, 0)) == 0) {
  52.         DebugStr ("\pNewGraphicsClient failed");
  53.         return false;
  54.     }
  55.  
  56.     /* Initialize QuickDraw GX graphics */
  57.     GXEnterGraphics ();
  58.  
  59.     GXSetValidation (gxNoValidation);
  60.     SetGraphicsLibraryErrors ();
  61.     SetGraphicsLibraryNotices();
  62.     InitCommonColors ();
  63.  
  64.     MaxApplZone();
  65.     MoreMasters();
  66.     MoreMasters();
  67.  
  68.     InitGraf (&thePort);
  69.     InitFonts ();
  70.     FlushEvents (everyEvent, 0);
  71.     InitWindows ();
  72.     InitMenus ();
  73.     InitDialogs (nil);
  74.     InitCursor ();
  75.  
  76.     return true;
  77.  
  78. }    /* InitApp */
  79.  
  80.  
  81.     void
  82. ExitApp (void)
  83. {
  84.     gxViewPort    parent;
  85.  
  86.     if (gWindow)
  87.     {
  88.         if (parent = GXGetWindowViewPort (gWindow))
  89.             GXDisposeViewPort (parent);
  90.         DisposeDialog (gWindow);
  91.     }
  92.  
  93.        DisposeCommonColors ();
  94.     GXExitGraphics ();
  95.     if (gClient)
  96.         GXDisposeGraphicsClient (gClient);
  97.  
  98. }    /* ExitApp */
  99.  
  100.  
  101.     void
  102. InitializeMenus (void)
  103. {
  104.     ghMainMenu = GetNewMBar (menuBarRsrcID);
  105.     SetMenuBar (ghMainMenu);
  106.     AddResMenu (GetMHandle (appleMenuRsrcID), 'DRVR');
  107.     DrawMenuBar ();
  108.  
  109. }    /* InitializeMenus */
  110.  
  111.  
  112.     WindowPtr
  113. InitializeWindow (void)
  114. {
  115.     WindowPtr    pWindow;
  116.     Rect        bounds;
  117.     short        totalMargin;
  118.     Point        newPosition;
  119.     GDHandle    hGDevice;
  120.  
  121.     if ((pWindow = GetNewDialog (dialogRsrcID, nil, kWindowOnTop)) == nil)
  122.         return (nil);
  123.  
  124.     SetPort (pWindow);
  125.     SetDefaultViewPort (GXNewWindowViewPort (pWindow));
  126.  
  127.     /* Find the deepest screen */
  128.     SetRect (&bounds, -32767, -32767, 32767, 32767);
  129.     hGDevice = GetMaxDevice (&bounds);
  130.     bounds = (**hGDevice).gdRect;
  131.  
  132.     totalMargin = (bounds.right - bounds.left) - (pWindow->portRect.right - pWindow->portRect.left);
  133.     newPosition.h = bounds.left + (totalMargin >> 1);
  134.  
  135.     totalMargin = (bounds.bottom - bounds.top) - (pWindow->portRect.bottom - pWindow->portRect.top - kWindowTitleHeight);
  136.     newPosition.v = bounds.top + ((totalMargin + ((hGDevice == GetMainDevice ()) ? MBarHeight : 0)) >> 1);
  137.  
  138.     MoveWindow (pWindow, newPosition.h, newPosition.v, true);
  139.     ShowWindow (pWindow);
  140.  
  141.     gDragRect = screenBits.bounds;
  142.     return (pWindow);
  143.  
  144. }    /* InitializeWindow */
  145.  
  146.  
  147.     void
  148. DoContentClick (EventRecord *pEvent, WindowPtr pWindow)
  149. {
  150.     Boolean    doUpdateWindow;
  151.  
  152.     if (pWindow == nil)
  153.         return;
  154.  
  155.     if (FrontWindow () == pWindow)
  156.     {
  157.         if (pWindow == gWindow)
  158.             DoHitTestContent (pEvent, pWindow);
  159.     }
  160.     else
  161.     {
  162.         SetPort (pWindow);
  163.         SelectWindow (pWindow);
  164.     }
  165.  
  166. }    /*    DoContentClick  */
  167.  
  168.  
  169.     void
  170. DoMenuCommand (menuResult)
  171.     long        menuResult;
  172. {
  173.     short        menuID,
  174.                 itemNumber;
  175.  
  176.     if (! menuResult)
  177.         return;
  178.  
  179.     menuID       = HiWord (menuResult);
  180.     itemNumber = LoWord (menuResult);
  181.  
  182.     switch (menuID)
  183.     {
  184.         case appleMenuRsrcID:
  185.             switch (itemNumber)
  186.             {
  187.                 case itemAbout:
  188.                     Alert (aboutRsrcID, nil);
  189.                     break;
  190.  
  191.                 default:
  192. //                    DoDeskAcc (appleMenuRsrcID, itemNumber);
  193.                     SysBeep (1);
  194.                     break;
  195.             }
  196.             break;
  197.  
  198.         case fileMenuRsrcID:
  199.             switch (itemNumber)
  200.             {
  201.                 case itemQuit:
  202.                     gQuitApp = true;
  203.                     break;
  204.  
  205.                 default:
  206.                     SysBeep (1);
  207.                     break;
  208.             }
  209.             break;
  210.  
  211.         case metricsMenuRsrcID:
  212.             switch (itemNumber)
  213.             {
  214.                 case itemShowControlPoints:
  215.                 case itemShowLocalBounds:
  216.                     ToggleMetricsMenuItem (itemNumber, gWindow);
  217.                     break;
  218.  
  219.                 case itemAutoMouse:
  220.                     ToggleAutoMouse ();
  221.                     break;
  222.  
  223.                 default:
  224.                     SysBeep (1);
  225.                     break;
  226.             }
  227.             break;
  228.  
  229.         default:
  230.             break;
  231.     }  /*  switch (menuID)  */
  232.  
  233.     HiliteMenu (kHiliteAllMenus);
  234.  
  235. }    /*    DoMenuCommand  */
  236.  
  237.  
  238.     void
  239. DoGrowBox (EventRecord *pEvent, WindowPtr pWindow)
  240. {
  241.     long        newSize;
  242.     Rect        newRect,
  243.                 oldRect;
  244.  
  245.     oldRect = newRect = pWindow->portRect;
  246.  
  247.     if (newSize = GrowWindow (pWindow, pEvent->where, &screenBits.bounds))
  248.     {
  249.         newRect.right    = newRect.left + (short) newSize;
  250.         newRect.bottom    = newRect.top  + (short) (newSize >> 16);
  251.         SizeWindow (pWindow,
  252.                     newRect.right - newRect.left,
  253.                     newRect.bottom - newRect.top,
  254.                     true);
  255.         InvalRect (&oldRect);
  256.     }
  257.  
  258. }    /*    DoGrowBox  */
  259.  
  260.  
  261.     void
  262. DoZoomBox (EventRecord *pEvent, WindowPtr pWindow, short windowPart)
  263. {
  264.     if (TrackBox (pWindow, pEvent->where, windowPart))
  265.     {
  266.         ZoomWindow (pWindow, windowPart, true);
  267.         InvalRect (&pWindow->portRect);
  268.     }
  269.  
  270. }    /*    DoZoomBox  */
  271.  
  272.  
  273.     void
  274. DoNullEvent (EventRecord *pEvent)
  275. {
  276.     IdleHitTestWindow (gWindow, pEvent);
  277.     MoveAutoMouse ();
  278.  
  279. }    /*    DoNullEvent  */
  280.  
  281.  
  282.     void
  283. DoMouseDown (EventRecord *pEvent)
  284. {
  285.     short            windowPart;
  286.     WindowPtr        whichWindow;
  287.  
  288.     windowPart = FindWindow (pEvent->where, &whichWindow);
  289.  
  290.     switch (windowPart)
  291.     {
  292.     case inDesk:
  293.         break;
  294.  
  295.     case inMenuBar:
  296.         DoMenuCommand (MenuSelect (pEvent->where));
  297.         break;
  298.  
  299.     case inSysWindow:
  300.         SystemClick (pEvent, whichWindow);
  301.         break;
  302.  
  303.     case inContent:
  304.         DoContentClick (pEvent, whichWindow);
  305.         break;
  306.  
  307.     case inDrag:
  308.         DragWindow (whichWindow, pEvent->where, &gDragRect);
  309.         break;
  310.  
  311.     case inGrow:
  312.         DoGrowBox (pEvent, whichWindow);
  313.         break;
  314.  
  315.     case inGoAway:
  316.           if (TrackGoAway (whichWindow, pEvent->where))
  317.           {
  318.               if (whichWindow == gWindow)
  319.                 gQuitApp = true;
  320.             else
  321.                 DebugStr ("\pDoMouseDown: Clicked in window of unknown close box");
  322.           }
  323.         break;
  324.  
  325.     case inZoomIn:
  326.     case inZoomOut:
  327.         DoZoomBox (pEvent, whichWindow, windowPart);
  328.         break;
  329.  
  330.     default:
  331.         break;
  332. }    /* switch */
  333.  
  334. }    /*    DoMouseDown  */
  335.  
  336.  
  337.     void
  338. DoKeyStroke (EventRecord *pEvent)
  339. {
  340.     char            charCode;
  341.  
  342.     charCode = pEvent->message & charCodeMask;
  343.  
  344.     if (pEvent->modifiers & btnState)
  345.     {
  346.         /*--------------*/
  347.         /* Button is UP */
  348.         /*--------------*/
  349.         if (pEvent->modifiers & cmdKey)
  350.         {
  351.             /*-------------------*/
  352.             /* Command key    x100 */
  353.             /*-------------------*/
  354.             DoMenuCommand (MenuKey (charCode));
  355.         }
  356.     }
  357.  
  358. }    /*    DoKeyStroke  */
  359.  
  360.  
  361.     void
  362. DoUpdate (EventRecord *pEvent)
  363. {
  364.     GrafPtr            savedPort;
  365.     WindowPtr        pUpdateWindow;
  366.  
  367.     GetPort (&savedPort);
  368.  
  369.     pUpdateWindow = (WindowPtr) pEvent->message;
  370.  
  371.     SetPort (pUpdateWindow);
  372.     BeginUpdate (pUpdateWindow);
  373.  
  374.     EraseRgn (pUpdateWindow->visRgn);
  375.  
  376.     if (pUpdateWindow == gWindow)
  377.         UpdateHitTestWindow (gWindow);
  378.  
  379.     EndUpdate (pUpdateWindow);
  380.     SetPort (savedPort);
  381.  
  382. }    /*    DoUpdate  */
  383.  
  384.  
  385.     void
  386. DoActivate (EventRecord *pEvent)
  387. {
  388.     WindowPtr        pActiveWindow;
  389.     gxViewPort        activeChild;
  390.  
  391.     /*--------------------------------------*/
  392.     /* Get the window to de/activate        */
  393.     /* and its kind from the event message    */
  394.     /*--------------------------------------*/
  395.     pActiveWindow = (WindowPtr) pEvent->message;
  396.  
  397.     SetPort (pActiveWindow);
  398.  
  399.     /*-----------------*/
  400.     /* Activate window */
  401.     /*-----------------*/
  402.     if (pEvent->modifiers & activeFlag)
  403.     {
  404.         /* Enable/Disable items */
  405.     }
  406.     else
  407.     /*-------------------*/
  408.     /* Deactivate window */
  409.     /*-------------------*/
  410.     {
  411.         /* Enable/Disable items */
  412.     }
  413.  
  414. }    /*    DoActivate  */
  415.  
  416.  
  417.     void
  418. DoEvent  (void)
  419. {
  420.     EventRecord    theEvent;
  421.  
  422.     SystemTask ();        /* Handle desk accessories */
  423.  
  424.     GetNextEvent (everyEvent, &theEvent);
  425.  
  426.     switch (theEvent.what)
  427.     {
  428.     case nullEvent:
  429.         DoNullEvent (&theEvent);
  430.         break;
  431.  
  432.     case mouseDown:
  433.         DoMouseDown (&theEvent);
  434.         break;
  435.  
  436.     case keyDown:
  437.     case autoKey:
  438.         DoKeyStroke (&theEvent);
  439.         break;
  440.  
  441.     case updateEvt:
  442.         DoUpdate (&theEvent);
  443.         break;
  444.  
  445.     case activateEvt:
  446.         DoActivate (&theEvent);
  447.         break;
  448.  
  449.     case mouseUp:
  450.     case keyUp:
  451.     case diskEvt:
  452.     case networkEvt:
  453.     case driverEvt:
  454.     default:
  455.         break;
  456.     }  /* switch (theEvent.what)  */
  457.  
  458. }    /* DoEvent */
  459.  
  460.  
  461. main (void)
  462. {
  463.     gQuitApp = false;
  464.     if (InitApp ())
  465.     {
  466.         InitializeMenus ();
  467.         if (gWindow = InitializeWindow ())
  468.         {
  469.             if (InitializeHitTesting (gWindow))
  470.             {
  471.                 InitializeAutoMouse (globals.boxes);
  472.  
  473.                 while (! gQuitApp)
  474.                     DoEvent ();
  475.  
  476.                 CleanupHitTesting ();
  477.             }
  478.         }
  479.         ExitApp ();
  480.     }
  481.  
  482. }    /* main */
  483.